package com.bw.heishou.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.os.Parcelable;
import android.widget.LinearLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.heishou.Adapter.MyViewPageAdapter;
import com.bw.heishou.Bean.MyTab;
import com.bw.heishou.Fragment.MyFragment1;
import com.bw.heishou.Fragment.MyFragment2;
import com.bw.heishou.Fragment.MyFragment3;
import com.bw.heishou.Fragment.MyFragment4;
import com.bw.heishou.R;
import com.bw.lib_core.mvp.view.BaseActivity;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity  {

    private com.flyco.tablayout.CommonTabLayout activityMainComm;
    ArrayList<CustomTabEntity> tabEntitys=new ArrayList<>();
    private androidx.viewpager.widget.ViewPager activityMainPage;

    @Override
    public int bandlayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {


        activityMainComm = (CommonTabLayout) findViewById(R.id.activity_main_comm);
        activityMainPage = (ViewPager) findViewById(R.id.activity_main_page);
//        list.add(MyFragment1.class);
//        list.add(MyFragment2.class);
//        list.add(MyFragment3.class);
//        list.add(MyFragment4.class);
//        activityMainBottom = (BottomNavigationBar) findViewById(R.id.activity_main_bottom);
        //设置底部菜单栏的模式
//        activityMainBottom.setMode(BottomNavigationBar.MODE_FIXED);
//        activityMainBottom.setActiveColor("#F40B5B");
//        activityMainBottom.setInActiveColor("#A8A7A7");
//
//        //设置底部菜单栏的布局
//        activityMainBottom
//                .addItem(new BottomNavigationItem(R.drawable.item1,"首页"))
//                .addItem(new BottomNavigationItem(R.drawable.item2,"首页"))
//                .addItem(new BottomNavigationItem(R.drawable.item3,"首页"))
//                .addItem(new BottomNavigationItem(R.drawable.item4,"首页"))
//                .initialise();
//
//        activityMainBottom.setTabSelectedListener(this);
//        activityMainBottom.selectTab(0);

        List<Fragment> list=new ArrayList<>();
        list.add(new MyFragment1());
        list.add(new MyFragment2());
        list.add(new MyFragment3());
        list.add(new MyFragment4());
        activityMainPage.setAdapter(new MyViewPageAdapter(getSupportFragmentManager()
                ,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,list));

        //给底部菜单添加对应的属性  arg1    标题， arg2选中图片    arg3未选中图片
        tabEntitys.add(new MyTab("首页1",R.drawable.item3,R.drawable.item1));
        tabEntitys.add(new MyTab("首页2",R.drawable.item3,R.drawable.item1));
        tabEntitys.add(new MyTab("首页3",R.drawable.item3,R.drawable.item1));
        tabEntitys.add(new MyTab("首页4",R.drawable.item3,R.drawable.item1));

        //设置底部的数据源
        activityMainComm.setTabData(tabEntitys);
        //设置每次开启所有页面都加载
        activityMainPage.setOffscreenPageLimit(list.size());
        //监听器
        activityMainComm.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                //选中时将viewpage也改变
                activityMainPage.setCurrentItem(position);
            }
            @Override
            public void onTabReselect(int position) {
            }
        });
        //viewpage的监听器
        activityMainPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                //页面改变时将底部菜单也改变
                activityMainComm.setCurrentTab(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode== PictureSelector.SELECT_REQUEST_CODE){
            if (data==null){
            }else {
//                String data1 = data.getData().getPath();
//                if (data1!=null){
//                    Message message=new Message();
//                    message.what=100;
//                    message.obj=data1;
//                    MyFragment1.handler.sendMessage(message);
//                }else {
//                    ToastUtils.showShort("null");
//                }
                if (data!=null){
                    PictureBean parcelableExtra = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                    if (parcelableExtra!=null){
                        Uri uri = parcelableExtra.getUri();
                        Message message=new Message();
                        message.what=100;
                        message.obj=uri;
                        MyFragment1.handler.sendMessage(message);
                    }else {
                        ToastUtils.showShort("您未点击图片");
                    }
                }
//                if (data!=null){
//                    PictureBean parcelableExtra = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
//                    Uri uri = parcelableExtra.getUri();
//                }
            }
        }
    }
}